package com.lechi.web.service.impl;

import com.lechi.web.common.enums.ErrorCodeEnum;
import com.lechi.web.common.exception.ServiceException;
import com.lechi.web.constants.Constants;
import com.lechi.web.framework.log.IotExceptionLog;
import com.lechi.web.service.IIotDeviceCmdService;
import com.lechi.web.system.domain.IotDeviceCmdBo;
import com.lechi.web.system.domain.IotDeviceCmdMonitor;
import com.lechi.web.system.domain.IotDeviceCmdParse;
import com.lechi.web.system.domain.IotDeviceCmdSchedule;
import com.lechi.web.system.mapper.iot.IotDeviceCmdMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 读写指令配置Service业务层处理
 *
 * @author yangguang
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class IotDeviceCmdServiceImpl implements IIotDeviceCmdService {

    private final IotDeviceCmdMapper iotDeviceCmdMapper;

    /**
     * 查询读写指令配置列表
     *
     * @param iotDeviceCmdBo 读写指令配置
     * @return 读写指令配置
     */
    @Override
    public List<IotDeviceCmdBo> selectIotDeviceCmdList(IotDeviceCmdBo iotDeviceCmdBo) {
        try {
            return iotDeviceCmdMapper.selectIotDeviceCmdList(iotDeviceCmdBo);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "查询读写指令配置列表", method, iotDeviceCmdBo));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
    }

    /**
     * 查询监控页面列表
     *
     * @param iotDeviceCmdBo 读写指令配置
     * @return 读写指令配置
     */
    @Override
    public List<IotDeviceCmdMonitor> selectIotDeviceMonitorList(IotDeviceCmdBo iotDeviceCmdBo) {
        try {
            return iotDeviceCmdMapper.selectIotDeviceMonitorList(iotDeviceCmdBo);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "查询监控页面列表", method, iotDeviceCmdBo));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
    }

    @Override
    public List<IotDeviceCmdSchedule> selectIotDeviceCmdListForSchedule(Long deviceId) {
        try {
            return iotDeviceCmdMapper.selectIotDeviceCmdListForSchedule(deviceId);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "查询指令调度列表", method, deviceId));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
    }

    @Override
    public List<IotDeviceCmdParse> selectIotDeviceCmdListByOrder(Long deviceId) {
        try {
            return iotDeviceCmdMapper.selectIotDeviceCmdListByOrder(deviceId);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "查询指令生成解析规则列表", method, deviceId));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
    }

    /**
     * 新增读写指令配置
     *
     * @param iotDeviceCmdBo 读写指令配置
     */
    @Override
    public void insertIotDeviceCmd(IotDeviceCmdBo iotDeviceCmdBo) {
        iotDeviceCmdBo.setCreateTime(new Date());
        iotDeviceCmdBo.setDataBit(Constants.REGISTER_32);
        try {
            iotDeviceCmdMapper.insertIotDeviceCmd(iotDeviceCmdBo);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "新增读写指令配置", method, iotDeviceCmdBo));
            throw new ServiceException(ErrorCodeEnum.ADD_ERROR.getValue(), ErrorCodeEnum.ADD_ERROR.getKey());
        }
    }

    /**
     * 修改读写指令配置
     *
     * @param iotDeviceCmdBo 读写指令配置
     */
    @Override
    public void updateIotDeviceCmd(IotDeviceCmdBo iotDeviceCmdBo) {
        iotDeviceCmdBo.setCreateTime(new Date());
        try {
            iotDeviceCmdMapper.updateIotDeviceCmd(iotDeviceCmdBo);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "修改读写指令配置", method, iotDeviceCmdBo));
            if (e.getCause() instanceof SQLIntegrityConstraintViolationException) {
                throw new ServiceException(ErrorCodeEnum.UPDATE_ERROR.getValue(), ErrorCodeEnum.UPDATE_ERROR.getKey());
            } else {
                throw new ServiceException(ErrorCodeEnum.EDIT_ERROR.getValue(), ErrorCodeEnum.EDIT_ERROR.getKey());
            }
        }
    }

    /**
     * 修改读写指令配置排序
     *
     * @param iotDeviceCmdBo 读写指令配置
     */
    @Override
    public void updateIotCmdByAliasId(IotDeviceCmdBo iotDeviceCmdBo) {
        iotDeviceCmdBo.setCreateTime(new Date());
        try {
            iotDeviceCmdMapper.updateIotCmdByAliasId(iotDeviceCmdBo);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "修改读写指令配置排序", method, iotDeviceCmdBo));
            throw new ServiceException(ErrorCodeEnum.EDIT_ERROR.getValue(), ErrorCodeEnum.EDIT_ERROR.getKey());
        }
    }

    /**
     * 批量删除读写指令配置
     *
     * @param ids 需要删除的读写指令配置主键
     */
    @Override
    public void deleteIotDeviceCmdByIds(Long[] ids) {
        try {
            iotDeviceCmdMapper.deleteIotDeviceCmdByIds(ids);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "批量删除读写指令配置", method, ids));
            throw new ServiceException(ErrorCodeEnum.EDIT_ERROR.getValue(), ErrorCodeEnum.EDIT_ERROR.getKey());
        }
    }

    /**
     * 删除读写指令配置信息
     *
     * @param deviceId 读写指令配置主键
     */
    @Override
    public void deleteIotDeviceCmdByDeviceId(Long deviceId) {
        try {
            iotDeviceCmdMapper.deleteIotDeviceCmdByDeviceId(deviceId);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "通过deviceId删除读写指令配置", method, deviceId));
            throw new ServiceException(ErrorCodeEnum.EDIT_ERROR.getValue(), ErrorCodeEnum.EDIT_ERROR.getKey());
        }
    }

    /**
     * 删除读写指令配置信息
     *
     * @param sensorAliasId 读写指令配置主键
     */
    @Override
    public void deleteIotDeviceCmdBySensorAliasId(Long sensorAliasId) {
        try {
            iotDeviceCmdMapper.deleteIotDeviceCmdBySensorAliasId(sensorAliasId);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "通过sensorAliasId删除读写指令配置", method, sensorAliasId));
            throw new ServiceException(ErrorCodeEnum.EDIT_ERROR.getValue(), ErrorCodeEnum.EDIT_ERROR.getKey());
        }
    }

}
